package com.red.wine.adapter;

import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

 
public class RootAdapter extends BaseAdapter {

    private static final String TAG = "RootAdapter";
    private boolean loadImageWhenScrolling = false;
    private boolean initedScrollListener = false;
    private boolean busy = false;

    public interface IOnScrollingHandler {
        void showScreenShots(boolean showScreenShots);
    }

    private IOnScrollingHandler onScrollingHandler;

    public void setOnScrollingHandler(IOnScrollingHandler onScrollingHandler) {
        this.onScrollingHandler = onScrollingHandler;
    }

 
    public int getCount() {
        return 0;
    }

 
    public Object getItem(int position) {
        return null;
    }

  
    public long getItemId(int position) {
        return 0;
    }

  

   
    public View getView(int position, View convertView, ViewGroup parent) {
        if (!(parent instanceof ListView)) {
            return null;
        }
        if (!initedScrollListener && loadImageWhenScrolling) {
            initedScrollListener = true;
            final ListView listView = (ListView) parent;
            listView.setOnScrollListener(new OnScrollListener() {

                 
                public void onScrollStateChanged(AbsListView view,
                        int scrollState) {
                    if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
                        busy = false;
                        //notifyDataSetChanged();
                        notifyChanged(view);
                    } else {
                        busy = true;
                    }
                }

               
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    if (!loadImageWhenScrolling || onScrollingHandler == null) {
                        return;
                    }
                    if (firstVisibleItem == 0
                            && view.getChildAt(firstVisibleItem) != null
                            && view.getChildAt(firstVisibleItem).getTop() == 0) {
                        onScrollingHandler.showScreenShots(true);
                    } else {//
                        onScrollingHandler.showScreenShots(false);
                    }
                }
            });
        }
        return null;
    }

    protected void notifyChanged(AbsListView view) {
        notifyDataSetChanged();
    }

    public boolean isLoadImageWhenScrolling() {
        return loadImageWhenScrolling;
    }

    public void setLoadImageWhenScrolling(boolean loadCurrent) {
        this.loadImageWhenScrolling = loadCurrent;
    }

    public boolean isBusy() {
        return busy;
    }
}
